package com.dcbeida.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.dcbeida.LoadingActivity;
import com.dcbeida.MyApplication;
import com.dcbeida.activity.login.LoginActivity;
import com.dcbeida.activity.login.LoginWithCodeActivity;
import com.dcbeida.bean.User;
import com.dcbeida.utils.SharedPreferencesUtils;

import butterknife.ButterKnife;

/**
 * 基类的封装
 * Created by fancl on 2016/8/4.
 */
public abstract class BaseActivity extends AppCompatActivity {


    /**
     * 获取layout的id，具体由子类实现
     *
     * @return
     */
    protected abstract int getLayoutResId();

    public Activity mContext;

    protected final String TAG = BaseActivity.class.getSimpleName();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResId());
        mContext = this;
        ButterKnife.bind(this);

        if (MyApplication.widthPixels == 321) {
            Log.i("sean", "############ width初始化");
            init();
        }


        if(MyApplication.user ==null && !(this instanceof LoginWithCodeActivity) && !(this instanceof LoginActivity)){
            User user= SharedPreferencesUtils.getInstance(BaseActivity.this).getUser();
            if( user!= null) {
                MyApplication.user = user;
            }else if(!(this instanceof LoadingActivity)){
                showActivity(LoginWithCodeActivity.class,null);
                return;
            }
        }

        setData();
    }

    public void setData() {

    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    public void showActivity(Class<? extends Activity> goActivity, Bundle aBundle) {
        //跳转页面
        Intent intent = new Intent();
        intent.setClassName(this, goActivity.getName());

        if (null != aBundle) {
            intent.putExtras(aBundle);
        }
        startActivity(intent);
    }

    public void showActivityForResult(Class<? extends Activity> goActivity, Bundle aBundle, int requestCode) {
        Intent intent = new Intent();
        intent.setClassName(this, goActivity.getName());
        if (null != aBundle) {
            intent.putExtras(aBundle);
        }
        startActivityForResult(intent, requestCode);
    }


    /**
     * 提示信息
     *
     * @param aFormatMsg
     * @param aMsgArgs
     */
    public void showTip(String aFormatMsg, Object... aMsgArgs) {
        String outString = String.format(aFormatMsg, aMsgArgs);
        int duration = (outString.length() > 10) ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
        Toast.makeText(mContext, outString, duration).show();
    }


    /**
     * 初始化数据
     */
    private void init() {
        getDeviceIdentification(this);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        MyApplication.widthPixels = dm.widthPixels;

    }

    /**
     * 获得设备相关信息
     */
    public void getDeviceIdentification(Context context) {
        try {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            // the IMEI for GSM and the MEID or ESN for CDMA phones
            String imei = tm.getDeviceId();
            // the IMSI for a GSM phone.
            String imsi = tm.getSubscriberId();
            // 手机系统的唯一号码
            String android_id = android.provider.Settings.System.getString(context.getContentResolver(), "android_id");
            if (null != imei && !"".equals(imei.trim()))
                MyApplication.DUID = imei;
            else if (null != imsi && !"".equals(imsi.trim()))
                MyApplication.DUID = imsi;
            else if (null != android_id && !"".equals(android_id.trim()))
                MyApplication.DUID = android_id;

        } catch (Exception e) {

        }


    }


    /**
     * 隐藏软键盘
     *
     * @param activity
     */
    public void closeInput(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        // 隐藏软键盘
        imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);

    }
}
